var util = require('../../../utils/util.js');
var api = require('../../../config/api.js');



var app = getApp();

Page({
  data: {
    longitude:'',
    latitude:'',
    top5:[],
    mechanism:[],
    page: 1,
    size: 10
  },
  onLoad: function (options) {
    wx.showLoading({
      title: '加载中...',
    });
    var that = this;
    wx.getLocation({
      type: 'wgs84',
      success(res) {
        that.setData({
          longitude: res.longitude,
          latitude: res.latitude        
        });
        util.request(api.GetCoupon,{
          category: options.id,
          longitude: that.data.longitude,
          latitude: that.data.latitude,
          page: that.data.page,
          size: that.data.size
        }).then(function (res) {
          if (res.errno === 0) {
            var ticketList = res.data.ticket
            if (ticketList != undefined) {
              for (let i = 0; i < ticketList.length; i++) {
                ticketList[i].discountsAddTime = ticketList[i].discountsAddTime.substr(0, 10);
                ticketList[i].discountsEndTime = ticketList[i].discountsEndTime.substr(0, 10)
              }
            } else {
              ticketList = ""
            }
            that.setData({
              top5: ticketList,
              mechanism: res.data.mechanism
            })
          }
        })
      }
    })
    wx.hideLoading();

  },
  onReady: function () {
    
  },
  onShow: function () {

  },
  onHide: function () {
    // 页面隐藏

  },
  onUnload: function () {
    // 页面关闭
  },
    onReachBottom() {
    if (this.data.totalPages > this.data.page) {
      this.setData({
        page: this.data.page + 1
      });
      this.getFootprintList();
    } else {
      wx.showToast({
        title: '没有更多机构了',
        icon: 'none',
        duration: 2000
      });
      return false;
    }
  },
})